﻿using System;
using CompactMapper.Orm.Contracts;
using CompactMapper.Orm.Extensions;

namespace CompactMapper.Orm.UnitOfWork.Mappings
{
    public class LongKey : IKey
    {
        private readonly long _key;
        public static readonly LongKey EmptyLongKey = new LongKey(0);

        public LongKey(long key)
        {
            _key = key;
        }

        public bool Equals(IKey other)
        {
            if (other == null)
                return false;

            var otherLong = other as LongKey;
            if (otherLong == null)
                return false;

            if (!this.IsSet() && !otherLong.IsSet())
                return false;

            return _key.Equals(otherLong.Value);
        }

        public override bool Equals(object obj)
        {
            return Equals((IKey)obj);
        }

        public override int GetHashCode()
        {
            return _key.GetHashCode();
        }

        public bool IsSet
        {
            get { return this != EmptyLongKey; }
        }

        public object Value
        {
            get { return _key; }
        }
    }
}